package com.learn.monitor;

import com.learn.monitor.oshi.SystemHardwareInfoTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * 打印电脑变量：环境变量、系统变量
 *
 * @author wsh
 * @date 2022-04-03 22:19
 */
public class PrintComputerVariable {
    private static final Logger logger = LoggerFactory.getLogger(SystemHardwareInfoTest.class);

    /**
     * Java 获取环境变量
     * 1、System.getEnv()  获得全部的环境变量
     * 2、System.getEnv(key) 获得某个环境变量的值
     *
     * @author wsh
     * @date 2022/4/3
     */
    @DisplayName("打印环境变量")
    @Test
    public void printEnvVariable() {
        Map<String, String> envMap = System.getenv();
        assertNotNull(envMap);

        logger.info("----------打印环境变量 Begin...");
        for (Map.Entry<String, String> entry : envMap.entrySet()) {
            //logger.info("{}={}", entry.getKey(), entry.getValue());
            System.out.format("%30s = %s%n", entry.getKey(), entry.getValue());
        }
        logger.info("----------打印环境变量 End.");
    }

    /**
     * Java 获取系统变量
     * 1、System.getProperties()  获得全部的系统变量
     * 2、System.getProperties(key) 获得某个系统变量的值
     *
     * @author wsh
     * @date 2022/4/3
     */
    @DisplayName("打印环境变量")
    @Test
    public void printSystemVariable() {
        Properties prop = System.getProperties();
        assertNotNull(prop);

        logger.info("----------打印系统变量 Begin...");
        for (Map.Entry<Object, Object> entry : prop.entrySet()) {
            //logger.info("{}={}", entry.getKey(), entry.getValue());
            System.out.format("%30s = %s%n", entry.getKey(), entry.getValue());
        }
        logger.info("----------打印系统变量 End.");
    }
}
